import React, { Component } from "react";
// 导入models
import models from "@/models/Goods";

import { Form, Modal, Button, message, Input } from "antd";

const layout = {
  labelCol: { span: 4 },
  wrapperCol: { span: 20 },
};

class GoodsUpdate extends Component {
  state = {
    visible: false,
    confirmLoading: false,
  };

  render() {
    return (
      <>
        <Button type="primary" size="small" onClick={this.showModal}>
          修改分类
        </Button>
        <Modal
          title="Title"
          visible={this.state.visible}
          onOk={this.handleOk}
          confirmLoading={this.state.confirmLoading}
          onCancel={this.handleCancel}
        >
          <Form
            {...layout}
            name="basic"
            initialValues={{ remember: true }}
            onFinish={this.onFinish}
            onFinishFailed={this.onFinishFailed}
          >
            <Form.Item
              label="修改分类："
              name="name"
              rules={[{ required: true }]}
              initialValue={this.props.data.name}
            >
              <Input ref={(c) => (this.inpText = c)} />
            </Form.Item>
          </Form>
        </Modal>
      </>
    );
  }

  // 显示模态框
  showModal = () => {
    // console.log(this.props);
    this.setState({ visible: true });
  };

  // 点击确定
  handleOk = () => {
    this.setState({ confirmLoading: true });
    if (this.inpText.props.value) {
      setTimeout(() => {
        let obj = {
          categoryId: this.props.data._id,
          categoryName: this.inpText.props.value,
        };

        // 发送请求
        models.goodUpdate(obj).then((ret) => {
          if (ret.status === 0) {
            message.success("修改成功");
            this.setState({ visible: false, confirmLoading: false });
            this.props.show(this.props.page);
          } else {
            message.error("修改失败");
            this.setState({ visible: false, confirmLoading: false });
          }
        });
      }, 700);
    } else {
      message.error("类名不能为空!!!");
      this.setState({ confirmLoading: false });
    }
  };

  // 点击取消
  handleCancel = () => {
    // console.log("Clicked cancel button");
    this.setState({ visible: false });
  };
}

export default GoodsUpdate;
